package com.wbj.sensor

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.wbj.vpdirection.recycle.DirectionView2
import com.wbj.vpdirection.viewpager.DirectionView

class ViewPagerDirectionActivity : AppCompatActivity() {
    
    lateinit var view: DirectionView
    lateinit var view2: DirectionView2
    
    val compassAction = "com.wbj.compass"
    private val broadcastReceiver = object : BroadcastReceiver(){
        override fun onReceive(context: Context?, intent: Intent?) {
            if (intent?.action.equals(compassAction)){
                val data = intent?.extras?.getFloat("compass")?.toFloat()
                data.let {
                    Log.i("ViewPagerDirectionActivity","$data")
                    if (data != null) {
                        view.setAngle(data)
                        view2.setAngle(data)
                    }
                }
            }
        }
    }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_pager_direction)
        view = findViewById<DirectionView>(R.id.directionView)
        view2 = findViewById<DirectionView2>(R.id.directionView2)
       
        view.setOffsetScreenPageLimit(3)
        view.setPageMargin(0f)
    
        val filter = IntentFilter()
        filter.addAction(compassAction)
        this.registerReceiver(broadcastReceiver,filter)
    }
    
    
    override fun onDestroy() {
        super.onDestroy()
        this.unregisterReceiver(broadcastReceiver)
    }
    
}